const Ajv = require("ajv");
const ajv = new Ajv();
/**
 * api参数校验
 * */
module.exports = (app) => {
  let $schema = "http://json-schema.org/draft-07/schema#";
  return async (ctx, next) => {
    if (ctx.path.indexOf("/api") < 0) {
      return await next();
    }
    // 获取请求参数
    const { body, query, headers } = ctx.request;
    const { path, method, params } = ctx;
    app.logger.info(`[${method} ${path}] body: ${JSON.stringify(body)}`);
    app.logger.info(`[${method} ${path}] query: ${JSON.stringify(query)}`);
    app.logger.info(`[${method} ${path}] params: ${JSON.stringify(params)}`);
    app.logger.info(`[${method} ${path}] headers: ${JSON.stringify(headers)}`);
    const schema = app.routerSchema[path]?.[method.toLowerCase()];
    if (!schema) {
      return await next();
    }
    let valid = true;
    let validate;
    if (valid && headers && schema.headers) {
      schema.headers.$schema = $schema;
      validate = ajv.compile(schema.headers);
      valid = validate(headers);
    }
    if (valid && body && schema.body) {
      schema.body.$schema = $schema;
      validate = ajv.compile(schema.body);
      valid = validate(body);
    }
    if (valid && query && schema.query) {
      schema.query.$schema = $schema;
      validate = ajv.compile(schema.query);
      valid = validate(query);
    }
    console.log('params',ctx.params);
    if (valid && params && schema.params) {
      schema.params.$schema = $schema;
      validate = ajv.compile(schema.params);
      valid = validate(params);
    }
    if (!valid) {
      ctx.status = 200;
      ctx.body = {
        success: false,
        message: `request validate fail: ${ajv.errorsText(validate.errors)}`,
        code: 442,
      };
    }
    await next();
  };
};
